package com.xintonghua.bussiness.ui.home;

import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;

import com.alibaba.fastjson.JSONObject;
import com.xintonghua.base.utils.MyUtils;
import com.xintonghua.base.widget.ConfirmDialog;
import com.xintonghua.bussiness.BuildConfig;
import com.xintonghua.bussiness.R;
import com.xintonghua.bussiness.adapter.OrderHomeAdapter;
import com.xintonghua.bussiness.api.HttpCent;
import com.xintonghua.bussiness.api.manager.AuthManager;
import com.xintonghua.bussiness.base.BaseActivity;
import com.xintonghua.bussiness.bean.HomeTopDataBean;
import com.xintonghua.bussiness.bean.Message;
import com.xintonghua.bussiness.bean.ShopOrderListBean;
import com.xintonghua.bussiness.interfaces.HomeOrderInterface;
import com.xintonghua.bussiness.request.ShopOrderListRequest;
import com.xintonghua.bussiness.util.JsonUtil;
import com.xintonghua.bussiness.util.JumpUtils;
import com.xintonghua.bussiness.util.MyState;

import java.util.ArrayList;
import java.util.HashMap;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 作者：wanxianyi
 * 时间：2018/4/24.
 * 包名：com.xintonghua.bussiness.ui.home
 */

public class SelectOrderActivity extends BaseActivity {
    @BindView(R.id.tab_order)
    TabLayout tabOrder;
    @BindView(R.id.lv_order)
    RecyclerView lvOrder;
    private String time;
    private int mCurPage = 1;
    private int mCurUseStatus = 1;  //0已完成  ，1未完成（默认未完成）
    private String[] title = new String[]{"未服务", "已服务"};
    private OrderHomeAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_order_select);
        ButterKnife.bind(this);
        onSimpleActionBar();
        setSimpleActionBar("筛选订单");
        time = getIntent().getParcelableExtra(BuildConfig.FLAVOR);
        initUI();

    }

    @Override
    public void initUI() {
        super.initUI();
        for (String s : title) {
            tabOrder.addTab(tabOrder.newTab().setText(s));
        }
        lvOrder.setLayoutManager(new LinearLayoutManager(SelectOrderActivity.this));
        adapter = new OrderHomeAdapter(this, new ArrayList<ShopOrderListBean.ListBean>());
        adapter.setHomeOrderInterface(new HomeOrderInterface() {
            @Override
            public void OnCancelOrderButtonClistener(final ShopOrderListBean.ListBean bean, int position) {
                ConfirmDialog.showDialog(SelectOrderActivity.this, "提示", "是否取消订单？", "取消", "确定", null, new ConfirmDialog.OnRightListener() {
                    @Override
                    public void onClick(ConfirmDialog dialog) {
                        CancelOrder("" + bean.getId());
                    }
                });
            }

            @Override
            public void OnQueRenDaoDianClistener(ShopOrderListBean.ListBean bean, int position) {
                HashMap<String, String> map = new HashMap<>();
                map.put("title", "工作人员确认");
                map.put("msg", "确认后将认为客户已到店消费");
                JumpUtils.jumpActivityForResult(SelectOrderActivity.this, DialogQueRenQianZiActivity.class, bean, map, MyState.requestL3000, false);
            }
        });
        lvOrder.setAdapter(adapter);
        initEvent();
        getOrderListData();
    }

    private void initEvent() {
        tabOrder.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                if (tab.getText().equals("未服务")) {
                    mCurPage = 1;
                    mCurUseStatus = 1;
                } else if (tab.getText().equals("已服务")) {
                    mCurPage = 1;
                    mCurUseStatus = 2;
                }
                try {
                    adapter.clear();
                    adapter.notifyDataSetChanged();
                } catch (Exception e) {
                    e.printStackTrace();
                }

                getOrderListData();
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });
    }

    //取消订单
    public void CancelOrder(String orderid) {
        HttpCent.getInstance(SelectOrderActivity.this).cancelOrder(orderid, this, MyState.requestTwo);
    }

    private void getOrderListData() {
        ShopOrderListRequest request = new ShopOrderListRequest();
        request.setUserId(AuthManager.getId());
        request.setPageNumber(mCurPage);
        request.setPageSize(10);
        request.setStatus(mCurUseStatus);
        HttpCent.getInstance(SelectOrderActivity.this).shopOrderList(getIntent().getStringExtra(BuildConfig.FLAVOR), request, this, MyState.requestOne);

    }

    @Override
    public void dataBack(Object data, int order) {
        super.dataBack(data, order);
        switch (order) {
            case MyState.requestOne:
                ShopOrderListBean bean = JSONObject.parseObject(data.toString(), ShopOrderListBean.class);
                if (bean != null && bean.getList() != null && bean.getList().size() > 0) {
                    adapter.setList(bean.getList());
                    adapter.notifyDataSetChanged();

                } else {
                    adapter.setList(new ArrayList<ShopOrderListBean.ListBean>());
                }
                break;
            case MyState.requestTwo:
                try {
                    MyUtils.mToast(SelectOrderActivity.this, "操作成功！");
                    getOrderListData();
                } catch (Exception e) {
                    e.printStackTrace();
                }
                break;
        }
    }

    @Override
    public void onStart() {
        super.onStart();
        tabOrder.post(new Runnable() {
            @Override
            public void run() {
                MyUtils.setTabLine(SelectOrderActivity.this, tabOrder, 20, 20);
            }
        });
    }
}
